package com.yonyou.gmmc.service.pointactivity.service;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.marketing.common.exception.BizException;
import org.marketing.common.util.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.infoservice.filestore.FileStore;
import com.infoservice.filestore.FileStoreException;
import com.yonyou.gmmc.service.pointactivity.constant.PointActivityConstant;
import com.yonyou.gmmc.service.pointactivity.dao.TcSysConfigMapper;
import com.yonyou.gmmc.service.pointactivity.dao.TmOwnerCarRelationMapper;
import com.yonyou.gmmc.service.pointactivity.dao.TmPotentialUserMapper;
import com.yonyou.gmmc.service.pointactivity.dao.TrUserOwnerMapper;
import com.yonyou.gmmc.service.pointactivity.dao.TtLhasaActivityRecordMapper;

@Transactional
@Service("GoToLhasaActivityService")
public class GoToLhasaActivityService {

	private Logger logger = Logger.getLogger(GoToLhasaActivityService.class);

	@Autowired
	private TmPotentialUserMapper tmPotentialUserMapper;

	@Autowired
	private TrUserOwnerMapper trUserOwnerMapper;

	@Autowired
	private TmOwnerCarRelationMapper tmOwnerCarRelationMapper;

	@Autowired
	private TtLhasaActivityRecordMapper ttLhasaActivityRecordMapper;

	@Autowired
	private TcSysConfigMapper tcSysConfigMapper;

	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	private SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	private SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");

	/**
	 * 根据openId 判断 是不是车主,是不是要进入上传照片的界面,是不是要进入
	 * 
	 * @param openId
	 * @return
	 * @throws ParseException
	 */
	public synchronized Map<String, Object> goToLhasaInit(String openId) throws ParseException {

		Map<String, Object> resultMap = new HashMap<String, Object>();

		// 是车主才可以参加去拉萨的活动
		if (judgeOpenIdType(openId).intValue() == PointActivityConstant.USER_TYPE_3.intValue()) {

			List<Map<String, Object>> keyList = tcSysConfigMapper.viewTcSysConfigByKey(getKeyList());

			Date startDate = sdf.parse("2017-07-26");
			Date endDate = sdf.parse("2017-08-20");
			Date activityEndDate = sdf.parse("2017-08-20");
			Date currentDate = new Date();

			for (Map<String, Object> map : keyList) {
				if (PointActivityConstant.SYS_CONFIG_KEY_04.equals(map.get("SYS_CONFIG_KEY").toString())) {
					startDate = sdf.parse(map.get("SYS_CONFIG_VALUE").toString());
				} else if (PointActivityConstant.SYS_CONFIG_KEY_05.equals(map.get("SYS_CONFIG_KEY").toString())) {
					endDate = sdf.parse(map.get("SYS_CONFIG_VALUE").toString());
				} else if (PointActivityConstant.SYS_CONFIG_KEY_06.equals(map.get("SYS_CONFIG_KEY").toString())) {
					activityEndDate = sdf.parse(map.get("SYS_CONFIG_VALUE").toString());
				}
			}

			if (currentDate.getTime() < startDate.getTime()) {
				resultMap.put(PointActivityConstant.INIT_RESULT_TYPE, PointActivityConstant.RESULT_MAP_STATUS_05);
			} else if (currentDate.getTime() > endDate.getTime()) {
				resultMap.put(PointActivityConstant.INIT_RESULT_TYPE, PointActivityConstant.RESULT_MAP_STATUS_06);
			} else if (currentDate.getTime() > activityEndDate.getTime()) {
				resultMap.put(PointActivityConstant.INIT_RESULT_TYPE, PointActivityConstant.RESULT_MAP_STATUS_07);
			} else {
				// 查询是否存在领取的记录.
				List<Map<String, Object>> list = ttLhasaActivityRecordMapper.viewRecordByOpenId(openId);

				if (0 == list.size()) {
					// openId 关联的奖券.一张都不存在.停留在第一个页面
					resultMap.put(PointActivityConstant.INIT_RESULT_TYPE, PointActivityConstant.RESULT_MAP_STATUS_00);
				} else {
					for (Map<String, Object> map : list) {
						boolean effective = Boolean.parseBoolean(map.get("EFFECTIVE").toString());
						// 判断是否有效 true 有效
						if (effective) {
							resultMap.put(PointActivityConstant.INIT_RESULT_TYPE, PointActivityConstant.RESULT_MAP_STATUS_02);
							break;
						} else {
							if (PointActivityConstant.YES.toString().equals(map.get("IS_PROMPT").toString())) {
								resultMap.put(PointActivityConstant.INIT_RESULT_TYPE, PointActivityConstant.RESULT_MAP_STATUS_03);
							} else {
								resultMap.put(PointActivityConstant.INIT_RESULT_TYPE, PointActivityConstant.RESULT_MAP_STATUS_04);
								if (null != map.get("RECORD_ID") && !"".equals(map.get("RECORD_ID"))) {
									ttLhasaActivityRecordMapper.updateIsPromptStatus(Long.valueOf(map.get("RECORD_ID").toString()));
								}
							}
							continue;
						}
					}
				}
			}
		} else {
			resultMap.put(PointActivityConstant.INIT_RESULT_TYPE, PointActivityConstant.RESULT_MAP_STATUS_01);
		}
		return resultMap;
	}

	/**
	 * 抽奖时间获取
	 * 
	 * @param openId
	 * @return
	 * @throws ParseException
	 */
	public Map<String, Object> getLuckDrawDate() throws ParseException {

		List<Map<String, Object>> keyList = tcSysConfigMapper.viewTcSysConfigByKey(getKeyList());

		String first = "10:00:00";
		String second = "20:44:00";
		Integer continued = 5;

		for (Map<String, Object> map : keyList) {
			if (PointActivityConstant.SYS_CONFIG_KEY_01.equals(map.get("SYS_CONFIG_KEY").toString())) {
				first = map.get("SYS_CONFIG_VALUE").toString();
			} else if (PointActivityConstant.SYS_CONFIG_KEY_02.equals(map.get("SYS_CONFIG_KEY").toString())) {
				second = map.get("SYS_CONFIG_VALUE").toString();
			} else if (PointActivityConstant.SYS_CONFIG_KEY_03.equals(map.get("SYS_CONFIG_KEY").toString())) {
				continued = Integer.valueOf(map.get("SYS_CONFIG_VALUE").toString());
			}
		}

		Date currentDate = sdf1.parse(sdf1.format(new Date()));

		Date firstDateStart = sdf1.parse(sdf.format(new Date()) + " " + first);
		Date firstDateEnd = sdf1.parse(addMinute(firstDateStart, continued));

		Date secondDateStart = sdf1.parse(sdf.format(new Date()) + " " + second);
		Date secondDateEnd = sdf1.parse(addMinute(secondDateStart, continued));

		logger.info("=========第一场=========");
		logger.info(firstDateStart);
		logger.info(firstDateEnd);

		logger.info("=========第二场=========");
		logger.info(secondDateStart);
		logger.info(secondDateEnd);

		Map<String, Object> resultMap = new HashMap<String, Object>();

		if (currentDate.getTime() >= firstDateStart.getTime() && currentDate.getTime() <= firstDateEnd.getTime()) {
			// 当前时间是否大于第一场的开始时间并且小于第一场的结束时间 如果是,就是正在秒杀的持续时间内
			resultMap.put("SeckillIn", true);
			resultMap.put("SeckillSurplusTime", (firstDateEnd.getTime() - currentDate.getTime()) / (1000));
		} else if (currentDate.getTime() >= secondDateStart.getTime() && currentDate.getTime() <= secondDateEnd.getTime()) {
			// 当前时间是否大于第一场的开始时间并且小于第二场的结束时间 如果是,就是正在秒杀的持续时间内
			resultMap.put("SeckillIn", true);
			resultMap.put("SeckillSurplusTime", (secondDateEnd.getTime() - currentDate.getTime()) / (1000));
		} else if (currentDate.getTime() >= firstDateStart.getTime() && currentDate.getTime() <= secondDateStart.getTime()) {
			resultMap.put("SeckillIn", false);
			resultMap.put("SeckillSurplusTime", (secondDateStart.getTime() - currentDate.getTime()) / (1000));
		} else if (currentDate.getTime() >= firstDateEnd.getTime() && currentDate.getTime() >= secondDateEnd.getTime()) {
			resultMap.put("SeckillIn", false);
			resultMap.put("SeckillSurplusTime", (new Date(firstDateStart.getTime() + 24 * 3600 * 1000).getTime() - currentDate.getTime()) / (1000));
		} else {
			resultMap.put("SeckillIn", false);
			resultMap.put("SeckillSurplusTime", (firstDateStart.getTime() - currentDate.getTime()) / (1000));
		}

		return resultMap;
	}

	/**
	 * 抽奖方法
	 * 
	 * @param openId
	 * @return
	 * @throws ParseException
	 */
	public Map<String, Object> luckDraw(String openId) throws BizException {

		Long recordId = null;

		// 查到该openId下所有的奖券
		List<Map<String, Object>> list = ttLhasaActivityRecordMapper.viewRecordByOpenId(openId);

		for (Map<String, Object> map : list) {
			boolean effective = Boolean.parseBoolean(map.get("EFFECTIVE").toString());
			// 判断是否有效 true 有效
			if (effective) {
				recordId = Long.valueOf(map.get("RECORD_ID").toString());
				break;
			} else {
				continue;
			}
		}

		if (null != recordId)
			throw new BizException("已经抽取过奖券.");

		Date date = new Date();
		// Date date = sdf1.parse("2017-07-26 16:06:00");

		Map<String, Object> lotteryMap = ttLhasaActivityRecordMapper.getOneLottery(sdf1.format(date));

		int insertNum = 0;

		if (null != lotteryMap) {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("openId", openId);
			param.put("lotteryId", lotteryMap.get("LOTTERY_ID"));
			insertNum = ttLhasaActivityRecordMapper.luckDraw(param);
		}

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("insertNum", insertNum);

		return resultMap;
	}

	/**
	 * 获取用户有效的奖券
	 * 
	 * @return
	 * @throws ParseException
	 */
	public Map<String, Object> getUserLottery(String openId) throws ParseException {
		// 查到该openId下所有的奖券
		List<Map<String, Object>> list = ttLhasaActivityRecordMapper.viewRecordByOpenId(openId);

		for (Map<String, Object> map : list) {
			map.put("LOTTERY_ACTIVATION_END_SHOW", sdf2.format(sdf.parse(list.get(0).get("LOTTERY_ACTIVATION_END").toString())));
			boolean effective = Boolean.parseBoolean(map.get("EFFECTIVE").toString());
			if (effective)
				return map;
		}
		return null;
	}

	/**
	 * 上传照片
	 * 
	 * @param openId
	 * @param imgFiles
	 * @return
	 * @throws FileStoreException
	 * @throws IOException
	 */
	public Map<String, Object> uploadPhoto(String openId, HttpServletRequest req) throws Exception {

		Long recordId = null;
		Long lotteryId = null;
		// 查到该openId下所有的奖券
		List<Map<String, Object>> list = ttLhasaActivityRecordMapper.viewRecordByOpenId(openId);

		for (Map<String, Object> map : list) {
			boolean effective = Boolean.parseBoolean(map.get("EFFECTIVE").toString());
			// 判断是否有效 true 有效
			if (effective) {
				recordId = Long.valueOf(map.get("RECORD_ID").toString());
				lotteryId = Long.valueOf(map.get("LOTTERY_ID").toString());
				break;
			} else {
				continue;
			}
		}

		if (null == recordId)
			throw new BizException("未查询到奖券,请联系管理员.");

		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		List<MultipartFile> imgFiles = ((MultipartHttpServletRequest) req).getFiles("imgFile");

		for (int i = 0; i < imgFiles.size(); ++i) {
			MultipartFile imgFile = imgFiles.get(i);
			if (!imgFile.isEmpty()) {
				if (isImage(imgFile)) {
					// 获取文件大小
					Long fileSize = imgFile.getSize();
					// 获取上传文件的路径
					String uploadFilePath = imgFile.getOriginalFilename();
					// 截取上传文件的文件名
					String uploadFileName = uploadFilePath.substring(uploadFilePath.lastIndexOf('\\') + 1, uploadFilePath.indexOf('.'));
					// 截取上传文件的后缀
					String uploadFileSuffix = uploadFilePath.substring(uploadFilePath.indexOf('.') + 1, uploadFilePath.length());
					// 上传
					String imgName = FileStore.getInstance().write("nd01", "fs01", uploadFileName + "." + uploadFileSuffix, imgFile.getBytes());
					String imgURL = FileStore.getInstance().getDomainURL(imgName);
					logger.info("图片路径imgURL:" + imgURL);
					logger.info("文件上传成功");
					Map<String, Object> param = new HashMap<String, Object>();
					param.put("fileName", uploadFileName);
					param.put("fileNameMd5", imgName);
					param.put("fileSize", fileSize);
					param.put("fileUrl", imgURL);
					param.put("openId", openId);
					param.put("lotteryId", lotteryId);
					ttLhasaActivityRecordMapper.uploadPhoto(param);
					ttLhasaActivityRecordMapper.updateEnableDate(recordId);
					resultMap.put("uploadStatus", "true");
				} else {
					throw new BizException("不是图片");
				}
			} else {
				throw new BizException("不是图片");
			}
		}

		return resultMap;
	}

	/**
	 * 查询openId有效奖券下已上传的图片
	 * 
	 * @param openId
	 * @return
	 */
	public Map<String, Object> viewPhoto(String openId) {

		Long recordId = null;

		// 查到该openId下所有的奖券
		List<Map<String, Object>> list = ttLhasaActivityRecordMapper.viewRecordByOpenId(openId);

		for (Map<String, Object> map : list) {
			boolean effective = Boolean.parseBoolean(map.get("EFFECTIVE").toString());
			// 判断是否有效 true 有效
			if (effective) {
				recordId = Long.valueOf(map.get("RECORD_ID").toString());
				break;
			} else {
				continue;
			}
		}

		if (null == recordId)
			throw new BizException("未查询到奖券,请联系管理员.");

		Map<String, Object> resultMap = new HashMap<String, Object>();

		Map<String, Object> param = new HashMap<String, Object>();
		param.put("recordId", recordId);
		param.put("openId", openId);
		resultMap.put("imgList", ttLhasaActivityRecordMapper.viewPhoto(param));

		return resultMap;
	}

	/**
	 * task
	 */
	public void lotteryTask() {
		Date date = new Date();
		ttLhasaActivityRecordMapper.lotteryTasks(sdf1.format(date));
	}

	/**
	 * 将一个时间加上多少分钟
	 * 
	 * @param date
	 * @param minute
	 * @return
	 * @throws ParseException
	 */
	public String addMinute(Date date, Integer minute) throws ParseException {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, minute);
		return sdf1.format(calendar.getTime());
	}

	/**
	 * 根据openId 判断 是粉丝,会员,车主.
	 * 
	 * @param openId
	 * @return
	 */
	public Integer judgeOpenIdType(String openId) {

		Map<String, Object> potentialMap = queryPotentialUser(openId);

		Map<String, Object> memberMap = null != potentialMap ? queryMemberInfo(potentialMap.get("POTENTIAL_USER_ID").toString()) : null;

		Map<String, Object> vehcileMap = null != memberMap ? queryVehcile(memberMap.get("USER_ID").toString()) : null;

		Integer userType = returnTypeByOpenId(potentialMap, memberMap, vehcileMap);

		logger.info("去拉萨活动获取到的的openId为:" + openId + ",根据openId判断出用户类型是:" + userType);

		return userType;
	}

	/**
	 * 查询潜客
	 * 
	 * @param openId
	 * @return
	 */
	public Map<String, Object> queryPotentialUser(String openId) {
		return tmPotentialUserMapper.queryPotentialUserByOpenId(openId);
	}

	/**
	 * 查询会员
	 * 
	 * @param potentialUserId
	 * @return
	 */
	public Map<String, Object> queryMemberInfo(String potentialUserId) {
		return trUserOwnerMapper.queryMemberInfoByPotentialUserId(potentialUserId);
	}

	/**
	 * 查询车辆
	 * 
	 * @param userId
	 * @return
	 */
	public Map<String, Object> queryVehcile(String userId) {
		return tmOwnerCarRelationMapper.queryVehcileByMemberId(userId);
	}

	/**
	 * 根据三个map判断是车主还是粉丝还是会员
	 * 
	 * @param potentialMap
	 * @param memberMap
	 * @param vehcileMap
	 * @return
	 */
	public Integer returnTypeByOpenId(Map<String, Object> potentialMap, Map<String, Object> memberMap, Map<String, Object> vehcileMap) {

		return null != potentialMap ? null != memberMap ? null != vehcileMap ? PointActivityConstant.USER_TYPE_3 : PointActivityConstant.USER_TYPE_2 : PointActivityConstant.USER_TYPE_1 : PointActivityConstant.USER_TYPE_0;
	}

	/**
	 * 查询TC_SYS_CONFIG表时,传入的条件
	 * 
	 * @return
	 */
	public List<String> getKeyList() {
		List<String> keyList = new ArrayList<String>();
		keyList.add(PointActivityConstant.SYS_CONFIG_KEY_01);
		keyList.add(PointActivityConstant.SYS_CONFIG_KEY_02);
		keyList.add(PointActivityConstant.SYS_CONFIG_KEY_03);
		keyList.add(PointActivityConstant.SYS_CONFIG_KEY_04);
		keyList.add(PointActivityConstant.SYS_CONFIG_KEY_05);
		keyList.add(PointActivityConstant.SYS_CONFIG_KEY_06);
		return keyList;
	}

	/**
	 * 奖券初始化
	 * 
	 * @param startDate
	 * @param endDate
	 * @param activityStartDate
	 * @param activityEndDate
	 * @param oneDayNum
	 * @param isAgain
	 * @param isAutomatic
	 * @throws Exception
	 */
	public void lotteryInit(String startDate, String endDate, String activityStartDate, String activityEndDate, Integer oneDayNum, boolean isAgain, boolean isAutomatic) throws Exception {

		if (isAutomatic) {
			ttLhasaActivityRecordMapper.deleteLottery();

			for (int i = 0; i < 3; i++) {
				if (i == 0) {
					startDate = "2017-07-26";
					endDate = "2017-08-01";
					activityStartDate = "2017-08-01";
					activityEndDate = "2017-08-10";
					oneDayNum = 20;
					generateLottery(startDate, endDate, activityStartDate, activityEndDate, oneDayNum);
				} else if (i == 1) {
					startDate = "2017-08-01";
					endDate = "2017-08-11";
					activityStartDate = "2017-08-11";
					activityEndDate = "2017-08-20";
					oneDayNum = 16;
					generateLottery(startDate, endDate, activityStartDate, activityEndDate, oneDayNum);
				} else {
					startDate = "2017-08-11";
					endDate = "2017-08-21";
					activityStartDate = "2017-08-21";
					activityEndDate = "2017-08-31";
					oneDayNum = 2;
					generateLottery(startDate, endDate, activityStartDate, activityEndDate, oneDayNum);
				}
			}
		} else {

			if (CheckUtil.NullOrEmpty(startDate))
				throw new BizException("isAutomatic等于false时,startDate参数必填.格式2017-07-26");
			if (CheckUtil.NullOrEmpty(endDate))
				throw new BizException("isAutomatic等于false时,endDate参数必填.格式2017-07-26");
			if (CheckUtil.NullOrEmpty(activityStartDate))
				throw new BizException("isAutomatic等于false时,activityStartDate参数必填.格式2017-07-26");
			if (CheckUtil.NullOrEmpty(activityEndDate))
				throw new BizException("isAutomatic等于false时,activityEndDate参数必填.格式2017-07-26");
			if (CheckUtil.NullOrEmpty(oneDayNum))
				throw new BizException("isAutomatic等于false时,oneDayNum参数必填.格式20");
			if (CheckUtil.NullOrEmpty(isAgain))
				throw new BizException("isAutomatic等于false时,startDate参数必填.格式true/false");

			if (isAgain)
				ttLhasaActivityRecordMapper.deleteLottery();
			generateLottery(startDate, endDate, activityStartDate, activityEndDate, oneDayNum);
		}
	}

	/**
	 * 生成奖券
	 * 
	 * @param startDate
	 * @param endDate
	 * @param activityStartDate
	 * @param activityEndDate
	 * @param oneDayNum
	 * @throws ParseException
	 */
	public void generateLottery(String startDate, String endDate, String activityStartDate, String activityEndDate, Integer oneDayNum) throws ParseException {

		List<Map<String, Object>> keyList = tcSysConfigMapper.viewTcSysConfigByKey(getKeyList());

		String first = "10:00:00";
		String second = "16:00:00";
		Integer continued = 5;

		for (Map<String, Object> map : keyList) {
			if (PointActivityConstant.SYS_CONFIG_KEY_01.equals(map.get("SYS_CONFIG_KEY").toString())) {
				first = map.get("SYS_CONFIG_VALUE").toString();
			} else if (PointActivityConstant.SYS_CONFIG_KEY_02.equals(map.get("SYS_CONFIG_KEY").toString())) {
				second = map.get("SYS_CONFIG_VALUE").toString();
			} else if (PointActivityConstant.SYS_CONFIG_KEY_03.equals(map.get("SYS_CONFIG_KEY").toString())) {
				continued = Integer.valueOf(map.get("SYS_CONFIG_VALUE").toString());
			}
		}

		int days = (int) ((sdf.parse(endDate).getTime() - sdf.parse(startDate).getTime()) / (1000 * 3600 * 24));

		for (int i = 0; i < days; i++) {
			for (int j = 0; j < oneDayNum; j++) {
				Map<String, Object> map = new HashMap<String, Object>();
				if (j % 2 == 0) {
					Date firstDateStart = sdf1.parse(startDate + " " + first);
					Date firstDateEnd = sdf1.parse(addMinute(firstDateStart, continued));
					map.put("lotteryReceiveStart", sdf1.format(firstDateStart));
					map.put("lotteryReceiveEnd", sdf1.format(firstDateEnd));
					map.put("lotteryActivationStart", activityStartDate);
					map.put("lotteryActivationEnd", activityEndDate);
				} else {
					Date secondDateStart = sdf1.parse(startDate + " " + second);
					Date secondDateEnd = sdf1.parse(addMinute(secondDateStart, continued));
					map.put("lotteryReceiveStart", sdf1.format(secondDateStart));
					map.put("lotteryReceiveEnd", sdf1.format(secondDateEnd));
					map.put("lotteryActivationStart", activityStartDate);
					map.put("lotteryActivationEnd", activityEndDate);
				}
				ttLhasaActivityRecordMapper.insertLottery(map);
			}
			Date date = sdf.parse(startDate);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.DATE, 1);
			startDate = sdf.format(cal.getTime());
		}
	}

	/**
	 * 判断是否图片
	 * 
	 * @param imgFile
	 * @return
	 * @throws IOException
	 */
	public static boolean isImage(MultipartFile imgFile) throws IOException {
		BufferedImage image = ImageIO.read(imgFile.getInputStream());
		if (image != null) {// 如果image=null 表示上传的不是图片格式
			return true;
		} else {
			return false;
		}
	}

	public void goToLhasaVisitRecord(String openId) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("openId", openId);
		ttLhasaActivityRecordMapper.goToLhasaVisitRecord(param);
	}

	public String getActivationEndDate() throws ParseException {

		String defaults = "2017年08月31日";

		Date date = new Date();
		
		List<Map<String, Object>> list = ttLhasaActivityRecordMapper.getActivationEndDate(sdf.format(date));
		if (0 == list.size())
			return defaults;

		for (Map<String, Object> map : list) {
			if ("T".equals(map.get("S").toString())) {
				return sdf2.format(sdf.parse(map.get("END_DATE").toString()));
			}
		}

		return defaults;
	}

}
